package game;

import domain.Food;
import domain.Snake;
import util.Direction;
import util.Node;

import javax.swing.*;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.LinkedList;
import java.util.Timer;
import java.util.TimerTask;

public class GameJFrame extends JFrame implements KeyListener {

    private JPanel jPanel;
    private Snake snake;
    private Food food;
    private Timer timer;


    public GameJFrame() {
        //初始化游戏窗口
        initJFrame();
        //初始化蛇
        initSnake();
        //初始化食物
        initFood();
        //初始化游戏界面
        initJPanel();
        //初始化定时器
        initTimer();
        //设置界面可见
        setVisible(true);
    }

    private void initTimer() {
        timer = new Timer();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                Node head=snake.getBody().getFirst();
                //check();
                snake.move();
                if(head.getX()==food.getFood().getX()&&head.getY()==food.getFood().getY()){
                    snake.eat(food);
                    food.getFood().random(snake);
                }
                jPanel.repaint();
            }
        };
        timer.scheduleAtFixedRate(timerTask, 0, 100);
    }

    private void initFood() {
        food = new Food();
    }

    private void initSnake() {
        snake = new Snake();
    }

    private void initJPanel() {
        jPanel = new JPanel() {
            @Override
            public void paint(Graphics g) {

                g.clearRect(0, 0, 1015, 640);

                for (int i = 0; i < 51; i++) {
                    g.drawLine(i * 20, 0, i * 20, 600);
                }
                for (int i = 0; i < 31; i++) {
                    g.drawLine(0, i * 20, 1020, i * 20);
                }
                LinkedList<Node> body = snake.getBody();
                for (Node node : body) {
                    g.fillRect(node.getX() * 20, node.getY() * 20, 20, 20);
                }
                g.fillRect(food.getFood().getX()*20,food.getFood().getY()*20,20,20);
            }
        };
        this.getContentPane().add(jPanel);
    }

    private void initJFrame() {
        this.setIconImage(Toolkit.getDefaultToolkit().getImage("image\\snake.png"));
        setSize(1015, 640);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setAlwaysOnTop(true);
        //setLayout(null);
        setLocationRelativeTo(null);
        setResizable(false);
        setTitle("贪吃蛇小游戏");
        this.addKeyListener(this);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        if(key==37&&snake.getDir()!= Direction.RIGHT){
            System.out.println("按下了←键");
            snake.setDir(Direction.LEFT);
        }
        if(key==38&&snake.getDir()!= Direction.DOWN){
            System.out.println("按下了↑键");
            snake.setDir(Direction.UP);
        }
        if(key==39&&snake.getDir()!= Direction.LEFT){
            System.out.println("按下了→键");
            snake.setDir(Direction.RIGHT);
        }
        if(key==40&&snake.getDir()!= Direction.UP){
            System.out.println("按下了↓键");
            snake.setDir(Direction.DOWN);
        }

    }


    @Override
    public void keyReleased(KeyEvent e) {

    }
}
